<template>
  <el-dialog :visible="showDialog" :title="showTitle" width="40%" @close="off">
    <el-form label-width="100px" :model="form" :rules="rules" ref="form">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入部门名称" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" placeholder="请输入部门编码" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="form.manager"
          placeholder="请选择负责人"
          @focus="getployess"
        >
          <el-option
            v-for="item in employees"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          type="textarea"
          v-model="form.introduce"
          placeholder="请输入部门介绍"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-row type="flex" justify="center">
        <el-button type="primary" @click="add">确定</el-button>
        <el-button @click="close">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import {
  getDpmList,
  addDepartment,
  getDepartDetail,
  updateDepartment,
} from "@/api/departments";
import { getEmployeeSimple } from "@/api/employees";
export default {
  // 接收父组件的弹框显示状态
  props: {
    showDialog: {
      type: Boolean,
      default: false,
    },
    // 接收部门数据
    node: {
      type: Object,
      required: true,
    },
  },

  data() {
    const nickName = async (rule, value, callback) => {
      // 获取部门最新数据
      const { depts } = await getDpmList();
      if(this.form.id){
        if(this.node.name === value){
          return callback()
        }
      }
      // 获取同级部门
      const result = depts.filter((item) => item.pid === this.node.id);
      // 判断部门名是否重复
      const isRepeat = result.some((item) => item.name === value);
      isRepeat
        ? callback(new Error(`同级部门下${value}已经存在了`))
        : callback();
    };
    const nickCode = async (rule, value, callback) => {
      // 获取部门最新数据
      const { depts } = await getDpmList();
      if(this.form.id){
        if(this.form.code  === value ){
          return callback()
        }
      }
      // 获取同级部门
      const isRepeat = depts.some((item) => item.code === value);
      isRepeat ? callback(new Error(`已经有编码${value}的部门了`)) : callback();
    };
    return {
      form: {
        name: "",
        manager: "",
        introduce: "",
        code: "",
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "部门名称不能为空", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "部门名称要求1-50个字符",
            trigger: "blur",
          },
          { validator: nickName, trigger: "blur" },
        ],
        code: [
          { required: true, message: "部门编码不能为空", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "部门编码要求1-50个字符",
            trigger: "blur",
          },
          { validator: nickCode, trigger: "blur" },
        ],
        manager: [
          { required: true, message: "部门负责人不能为空", trigger: "blur" },
        ],
        introduce: [
          { required: true, message: "部门介绍不能为空", trigger: "blur" },
          {
            min: 1,
            max: 300,
            message: "部门介绍要求1-50个字符",
            trigger: "blur",
          },
        ],
      },
      // 员工数据
      employees: [],
    };
  },
  methods: {
    off() {
      // 弹框状态
      this.$emit("update:showDialog", false);
      // 清除表单及检验结果
      this.$refs.form.resetFields();
    },
    // 获取员工信息
    async getployess() {
      const res = await getEmployeeSimple();
      this.employees = res;
    },
    // 提交表单
    add() {
      this.$refs.form.validate(async (valid) => {
        if (!valid) return;
        if (this.form.id) {
          await updateDepartment(this.form);
        } else {
          await addDepartment({
            ...this.form,
            pid: this.node.id,
          });
        }

        this.$message.success("提交成功");
        this.$emit("update:showDialog");
        this.$emit("getList");
      });
    },
    close() {
      this.form = {
        name: "",
        manager: "",
        introduce: "",
        code: "",
      };
      // 关闭弹层
      this.$emit("update:showDialog", false);
      // 重置表单
      this.$refs.form.resetFields();
    },
    // 获取部门详情
    async getDepartDetail(id) {
      this.form = await getDepartDetail(id);
    },
  },
  computed: {
    showTitle() {
      return this.form.id ? "编辑部门" : "新增子部门";
    },
  },
};
</script>
